import Vue from "vue";
import Vuex from "vuex";

//1.应用Vuex
Vue.use(Vuex);

//2.实例化Vuex仓库
const store = new Vuex.Store({
  //数据,state的值必须是一个对象!!!! 对象中保存我们想要的数据
  state: {
    num: 4,
  },

  getters: {
    doubleNum(state) {
      return state.num * 2;
    },
  },

  mutations: {
    //每次加1
    increment(state) {
      state.num++;
    },

    //每次加n
    incrementN(state, { n }) {
      state.num += n;
    },

  },

  actions: {
    incrementWait({ commit }) {

      setTimeout(() => {
        commit("increment");
      }, 1000);
    },

    incrementNWait({ commit }, payload) {
      setTimeout(() => {
        commit("incrementN", payload);
      }, 1000);
    },
  },
});

//3.暴露store给入口文件
export default store;
